package com.runen.infinitemovement.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.ColumnAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ActivityAPI;
import com.runen.infinitemovement.model.entity.CollageInfoEntity;
import com.runen.infinitemovement.model.entity.ColumnEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.tool.notchtool.NotchTools;
import com.runen.infinitemovement.tool.notchtool.core.NotchProperty;
import com.runen.infinitemovement.tool.notchtool.core.OnNotchCallBack;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ColumnActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    @BindView(R.id.iv_finish)
    ImageView ivFinish;
    @BindView(R.id.iv_header)
    ImageView ivHeader;
    @BindView(R.id.tv_activity_name)
    TextView tvActivityName;
    @BindView(R.id.tv_activity_num)
    TextView tvActivityNum;
    @BindView(R.id.rv_column)
    RecyclerView rvColumn;
    @BindView(R.id.iv_cover)
    ImageView ivCover;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.nev_view)
    NestedScrollView nevView;
    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    private ColumnAdapter columnAdapter;
    private List<CollageInfoEntity.DataBean> stringList = new ArrayList<>();
    private String subjectId;
    private int page = 2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_column;
    }

    @SuppressLint("NewApi")
    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //适配刘海屏
        NotchTools.getFullScreenTools().translucentStatusBar(this, new OnNotchCallBack() {
            @Override
            public void onNotchPropertyCallback(NotchProperty notchProperty) {
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rlTitle.getLayoutParams();
                layoutParams.height = CommonUtil.dp2px(ColumnActivity.this, 154) + CommonUtil.getStatusBarHeight(ColumnActivity.this);
                rlTitle.setLayoutParams(layoutParams);
                int padding = CommonUtil.dp2px(ColumnActivity.this, 12);
                ivFinish.setPadding(padding, CommonUtil.getStatusBarHeight(ColumnActivity.this)
                        , padding, padding);
            }
        });
        llTitle.setVisibility(View.GONE);
        int h = CommonUtil.getStatusBarHeight(this);
        llTitle.setPadding(0, h, 0, 0);
        srl.setOnRefreshListener(this);
        subjectId = getIntent().getStringExtra("subjectId");
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvColumn.setLayoutManager(layoutManager);
        columnAdapter = new ColumnAdapter(this);
        columnAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvColumn.setAdapter(columnAdapter);

        columnAdapter.getLoadMoreModule().setAutoLoadMore(false);
        columnAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Intent intent = new Intent(ColumnActivity.this, ColumnDetailsActivity.class);
                intent.putExtra("informationId", columnAdapter.getData().get(position).informationId);
                startActivity(intent);
            }
        });

        initData();
        iniLoadMore();
        nevView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY > 60) {
                    llTitle.setVisibility(View.VISIBLE);
                } else {
                    llTitle.setVisibility(View.GONE);
                }
            }
        });
    }

    private void iniLoadMore() {
        columnAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                onLoadMoreInfo();
            }
        });
        columnAdapter.getLoadMoreModule().setAutoLoadMore(true);
        columnAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    private void initData() {
        srl.setRefreshing(true);
        String string = "?limit=20&page=1&subjectId=%s";
        new ActivityAPI().onColumn(String.format(string, subjectId), new BaseResultCallback<ColumnEntity>() {
            @Override
            public void onResponse(ColumnEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null) {
//                        GlideUtils.loadImage(ColumnActivity.this, response.data.cover, ivCover);
//                        GlideUtils.loadImage(ColumnActivity.this, response.data.cover, ivHeader);
                        tvActivityName.setText(response.data.subjectName);
                        topTitle.setText(response.data.subjectName);
                        String str = "%s篇内容·%s浏览";
                        tvActivityNum.setText(String.format(str, response.data.contentNumber, response.data.readNumber));
                        if (response.data.inforMationListDTOList != null && response.data.inforMationListDTOList.size() > 0) {
                            columnAdapter.setNewData(response.data.inforMationListDTOList);
                            if (columnAdapter.getData().size() >= response.data.contentNubmer) {
                                columnAdapter.getLoadMoreModule().loadMoreEnd(true);
                                columnAdapter.getLoadMoreModule().setEnableLoadMore(false);
                            }
                        }
                    }
                }
            }
        });
    }

    @OnClick({R.id.iv_finish, R.id.iv_header, R.id.top_btn_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_finish:
                finish();
                break;
            case R.id.iv_header:
                break;
            case R.id.top_btn_left:
                finish();
                break;
        }
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    public void onLoadMoreInfo() {
        String string = "?limit=20&page=%s&subjectId=%s";
        new ActivityAPI().onColumn(String.format(string, page, subjectId), new BaseResultCallback<ColumnEntity>() {
            @Override
            public void onResponse(ColumnEntity response) {
                if (response.code == 0) {
                    if (response.data.inforMationListDTOList != null && response.data.inforMationListDTOList.size() > 0) {
                        columnAdapter.addData(response.data.inforMationListDTOList);
                        if (columnAdapter.getData().size() >= response.data.contentNubmer) {
                            columnAdapter.getLoadMoreModule().loadMoreEnd(true);
                            columnAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            columnAdapter.getLoadMoreModule().loadMoreComplete();
                            columnAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                }
            }
        });
    }
}
